//
//  HJEmoticonCell.swift
//  HJWeiBo
//
//  Created by 夏航军 on 16/10/8.
//  Copyright © 2016年 夏航军. All rights reserved.
//

import UIKit

class HJEmoticonCell: UICollectionViewCell {
    
    var emoticons : [HJEmoticon]? {
        didSet{
            
            //解决Cell复用的问题
            for btn in buttonArray {
                btn.isHidden = true
            }
            
            for (index,em) in emoticons!.enumerated() {
                
                let btn = buttonArray[index]
                //给按钮设置图片或者文字
                btn.emoticon = em
                btn .isHidden = false
            }
        }
    }
    
    
    
    lazy var buttonArray : [HJEmoticonButtton] = [HJEmoticonButtton]()
    
    
    lazy var popoView : HJEmoticonPopoView = {
        let popo = HJEmoticonPopoView.loadPopoView()
        
        return popo
    }()

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        
     
        addChildButtons()
        
        //添加手势,用于pop效果
        let longPre = UILongPressGestureRecognizer(target: self, action: #selector(longPress(ges:)))
        
        contentView.addGestureRecognizer(longPre)
    }
    
    @objc private func longPress(ges: UILongPressGestureRecognizer) {
        //1. 根据手势触摸的点 --> 查找按钮  --> 模型数据  -> popoView需要模型数据
        let point = ges.location(in: contentView)
        guard  let btn = findEmoticon(point: point) else {
            return
        }
        //判断按钮是否是显示的
        if btn.isHidden == true {
            
            popoView.removeFromSuperview()
            return
        }
        switch ges.state {
        case .began,.changed:
            //显示popoView
            //添加到contentView上
            let window = UIApplication.shared.windows.last!
            //转换左边  按钮需要将坐标转换到哪一个视图上
            //转换的坐标填什么有规律: 转换自己的坐标的时候就是用bounds 如果转换父视图的坐标的就用frame
            let rect = btn.superview!.convert(btn.frame, to: window)
            //let rect =  btn.convert(btn.bounds, to: window)
            popoView.center.x = rect.midX
            popoView.frame.origin.y = rect.maxY - popoView.bounds.height
            //给表情按钮设置模型
            popoView.emoticonButton.emoticon = btn.emoticon
            window.addSubview(popoView)
            popoView.show()
        default:
            //移除popoView
            popoView.removeFromSuperview()
        }

    }
    
    //寻找手势所在的点在哪一个button上
    func findEmoticon(point: CGPoint) -> HJEmoticonButtton? {
        
        for btn in buttonArray {
            if btn.frame.contains(point) {
                return btn
            }
        }
        return nil
    }
    
    
    //添加button
    func addChildButtons() {
        
        //留给底部pageControl
        let bottomMargin : CGFloat = 30
        let btnW = KScreenW / 7
        let btnH = (emoticonKeyboardHeight - emoticonToolBarHeight - bottomMargin) / 3
        for i in 0..<emoticonPageCount {
            let btn = HJEmoticonButtton()
            
            //计算行 和 列
            let colIndex  = i % 7
            let rowIndex = i / 7
            
            let btnX = CGFloat(colIndex) * btnW
            let btnY = CGFloat(rowIndex) * btnH
            
            btn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
            
            //btn.backgroundColor = randomColor()
            
            //btn添加点击事件
            btn.addTarget(self, action: #selector(emoticonBtnDidClick(btn:)), for: .touchUpInside)
            //因为emoji不是图片,所以需要设置文字的大小显示emoji
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 32)
            //把按钮添加到数组中
            buttonArray.append(btn)
            
            contentView.addSubview(btn)
            
        }
        //添加删除按钮
        let deleteBtn = UIButton()
        deleteBtn.frame = CGRect(x: KScreenW - btnW, y: 2 * btnH, width: btnW, height: btnH)
        deleteBtn.setImage(#imageLiteral(resourceName: "compose_emotion_delete"), for: .normal)
        //添加删除按钮的监听事件
        deleteBtn.addTarget(self, action: #selector(deleteBtnDiDclick), for: .touchUpInside)
        contentView.addSubview(deleteBtn)

    }
    
    //图片按钮的点击事件
    func emoticonBtnDidClick(btn : HJEmoticonButtton) {
        
        //将点击的表情存到本地
        HJEmoticonTools.sharedEmoticonTools.saveRecentEmotion(em: btn.emoticon!)
    }
    
    //删除方法
    func deleteBtnDiDclick() {
        
    }
}
